package com.medusa.aps.business.modules.modeling.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.modeling.model.input.WorkingProcedureDTO;
import com.medusa.aps.business.modules.modeling.model.input.WorkingProcedureParam;
import com.medusa.aps.business.modules.modeling.model.output.WorkingProcedureVO;
import com.medusa.aps.business.modules.modeling.mp.service.WorkingProcedureService;
import com.medusa.aps.business.util.PercentageScheduleUtils;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工序控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:53
 */
@Validated
@RestController
@RequestMapping("/working/procedure")
@Tag(name = "工序控制层", description = "工序控制层")
public class WorkingProcedureController {
    /**
     * 服务对象
     */
    @Resource
    private WorkingProcedureService workingProcedureService;

    /**
     * 分页查询所有数据
     *
     * @param workingProcedure 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询所有工序", description = "分页查询所有工序")
    public Result<Page<WorkingProcedureVO>> selectWorkingProcedurePage(@Parameter(description = "查询参数") WorkingProcedureParam workingProcedure) {
        return Result.ok(workingProcedureService.selectWorkingProcedurePage(new Page<>(workingProcedure.getCurrent(), workingProcedure.getSize()), workingProcedure));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/detail")
    @Operation(summary = "通过主键查询单条数据", description = "通过主键查询单条数据")
    public Result<WorkingProcedureVO> detail(@RequestParam Integer id) {
        return Result.ok(this.workingProcedureService.detail(id));
    }

    /**
     * 新增数据
     *
     * @param workingProcedures 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据", description = "新增数据")
    public Result<Void> save(@RequestBody @Valid @Size(min = 1, message = "新增工序不能少于一条") List<WorkingProcedureDTO> workingProcedures) {
        workingProcedureService.saveWorkingProcedure(workingProcedures);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param workingProcedure 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改数据", description = "修改数据")
    public Result<Void> update(@RequestBody @Validated(Update.class) WorkingProcedureDTO workingProcedure) {
        workingProcedureService.updateWorkingProcedure(workingProcedure);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param processIds 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据", description = "删除数据")
    public Result<Void> delete(@RequestBody List<Integer> processIds) {
        workingProcedureService.removeWorkingProcedure(processIds);
        return Result.ok();
    }

    /**
     * 查询工序是否被工艺路线所使用
     *
     * @param id 工序id
     * @return 是否被工艺路线所使用 true ->存在，false->不存在
     */
    @GetMapping("/exists/process/route")
    @Operation(summary = "查询工序是否被工艺路线所使用", description = "查询工序是否被工艺路线所使用")
    public Result<Boolean> existsProcessRoute(@Parameter Integer id) {
        return Result.ok(workingProcedureService.existsProcessRoute(id));
    }
}

